
import { _decorator, Component, Label, Toggle } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('GUICheckbox')
export class GUICheckbox extends Component {

    @property(Toggle)
    toggle: Toggle | null = null;

    @property(Label)
    label: Label | null = null;

    private _clickFn: ((check: boolean) => void) | null = null;

    onLoad() {
        this.toggle?.node.on('toggle', this._onToggle, this);
    }

    onDestroy() {
        this.toggle?.node.off('toggle', this._onToggle, this);
    }

    private _onToggle(toggle: Toggle) {
        this._clickFn?.(toggle.isChecked);
    }

    public init(
        name: string,
        defaultCheck: boolean,
        clickFn: (check: boolean) => void,
    ) {
        this._clickFn = clickFn;
        if (this.toggle) {
            this.toggle.isChecked = defaultCheck;
        }
        if (this.label) {
            this.label.string = name;
        }
    }

}
